package com.dev.op.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dev.op.VO.ProblemPostVO;
import com.dev.op.VO.ResultVO;
import com.dev.op.VO.TestCaseVO;
import com.dev.op.entity.Problem;
import com.dev.op.entity.Submission;
import com.dev.op.entity.TestCase;
import com.dev.op.enums.ResultCode;
import com.dev.op.exceptions.BizException;
import com.dev.op.service.ProblemService;
import com.dev.op.mapper.ProblemMapper;
import com.dev.op.service.RedisService;
import com.dev.op.service.TestCaseService;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【problem】的数据库操作Service实现
 * @createDate 2025-03-23 11:54:58
 */
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem>
        implements ProblemService{

    @Autowired
    private TestCaseService testCaseService;

    @Autowired
    private ProblemMapper problemMapper;

    @Autowired
    RedisService redisService;

    @Autowired
    RBloomFilter<String> problemIdBloomFilter;


    @Override
    @Transactional
    public void saveWithTestCases(ProblemPostVO ppv) {
        Problem problem = new Problem();
        problem.setProblemTitle(ppv.getTitle());
        problem.setProblemDesc(ppv.getDescription());
        this.save(problem);
        problemIdBloomFilter.add(String.valueOf(problem.getProblemId()));
        List<TestCaseVO> ltcvos = ppv.getTestCases();
        for (TestCaseVO tvo : ltcvos) {
            TestCase tc = new TestCase();
            tc.setProblemId(problem.getProblemId());
            tc.setIsSample(tvo.isSample()?1:0);
            tc.setInputData(tvo.getInputData());
            tc.setOutputData(tvo.getOutputData());
            testCaseService.save(tc);
        }
    }

    @Override
    public Page<Problem> listProblems(int page,int size) {
        Page<Problem> problemPage = new Page<>(page, size);
        // 执行分页查询，返回分页结果
        return problemMapper.selectPage(problemPage,
               null);
    }

    @Override
    public ResultVO<Problem> getProblemById(int id) {
        //布隆过滤器
        //无效id 过滤
        if(!problemIdBloomFilter.contains(id+"")) {
            //System.out.println("拦截");
            return ResultVO.error(ResultCode.NOT_FOUND.message());
        }
        //先尝试向redis中查找对应的id
        Problem tryProblem = redisService.getProblem(String.valueOf(id));
        if(tryProblem != null) {
            //System.out.println("向redis取到数据直接返回");
            return ResultVO.success(tryProblem);
        }
        //查询一次，并向redis丢缓存，设置过期时间
        else {
            Problem problem = this.getById(id);
            if (problem == null) {
                throw new BizException(ResultCode.NOT_FOUND);
            }
            redisService.saveProblem(problem,36000);
            //System.out.println("保存至redis");
            return ResultVO.success(problem);
        }
    }
}




